﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class HobbyGuidAjaxDropdownController: Controller
    {
        private readonly IRepo<Hobby> repo;

        public HobbyGuidAjaxDropdownController(IRepo<Hobby> repo)
        {
            this.repo = repo;
        }

        public ActionResult GetItems(Guid? key)
        {
            var list = new List<SelectListItem> { new SelectListItem { Text = "not selected", Value = ""} };
            list.AddRange(
                repo.GetAll().Select(o => new SelectListItem
                                              {
                                                  Text = o.Name,
                                                  Value = o.IdGuid.ToString(),
                                                  Selected = o.IdGuid == key
                                              }));
            return Json(list);
        }
    }
}